package mahan.data.core;

import java.util.Date;
import java.util.List;

import mahan.data.model.CartLineItem;
import mahan.data.model.ShoppingCart;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

public class Util {
	public static BSONObject convert(CartLineItem item) {
		BasicBSONObject bso = new BasicBSONObject();
		bso.putAll(item.get());
		return bso;
	}

	public static CartLineItem getCartItem(BSONObject bso) {
		CartLineItem item = new CartLineItem();

		item.setAdded((Date) bso.get("add"));
		item.setItemType((String) bso.get("it"));
		item.setProductID((Integer) bso.get("pid"));
		item.setSellingPrice((Double) bso.get("sell"));
		item.setSize((Double) bso.get("sz"));
		return item;
	}

	public static double total(ShoppingCart cart) {
		double total = 0;
		List<CartLineItem> items = cart.getItems();
		for (CartLineItem item : items) {
			total += item.getSellingPrice();
		}
		return total;
	}
}